package com.enjoyor.soa.traffic.core.efoms.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.enjoyor.soa.traffic.core.efoms.domain.AssentFileInfo;
import com.enjoyor.soa.traffic.core.efoms.mapper.IAssentFileInfoMapper;
import com.enjoyor.soa.traffic.core.efoms.mapper.IAssetInfoMapper;
import com.enjoyor.soa.traffic.core.efoms.service.IAssentFileInfoService;
import com.enjoyor.soa.traffic.frame.logger.IPubLogMapper;
import com.enjoyor.soa.traffic.util.enums.log.LogInfoDic;
import com.enjoyor.soa.traffic.util.helper.TimeHelper;
import com.enjoyor.soa.traffic.util.pojo.ResultPojo;

@Service
@Transactional
@SuppressWarnings("rawtypes")
public class AssentFileInfoService implements IAssentFileInfoService{

	@Autowired
	private IAssentFileInfoMapper assentFileInfoMapper;
	
	@Autowired
	private IPubLogMapper pubLogMapper;

	/***
	 * 文件添加；
	 * @param assentFileInfoDto 文件对象 
	 * @param personName 操作人
	 * @return
	 */
	public ResultPojo insertAssentFileInfo(List<AssentFileInfo> list,String personName) {
		// TODO Auto-generated method stub
		if(list.size()>0){
			for(int i = 0;i<list.size();i++){
				AssentFileInfo assentFileInfo = list.get(i);
				String nowTime = TimeHelper.dateToLongString();
				assentFileInfo.setFileId(TimeHelper.getUniqueString());
				assentFileInfo.setFileTime(nowTime);
				try {
					assentFileInfoMapper.insertSelective(assentFileInfo);				
					pubLogMapper.saveNoramlLogInfo(personName, null, LogInfoDic.LOG_TRANSTYPE_ENUM.TRANSTYPE_INSERT.getCode(),
							String.format("添加附件编号:{%s} 操作成功!", new Object[] { list.get(i).getFileId()}),
							LogInfoDic.LOG_MODULAR_ENUM.EFOMS_ASSENT_MANAGEMENT.getMessage());
				} catch (Exception e) {
					// TODO: handle exception
					throw e;
				}
			}			
		}
		return new ResultPojo();
	}

	/***
	 * 附件删除
	 * @param fileId 附件编号
	 * @param personName 操作人
	 * @return
	 */
	public ResultPojo deleteAssentFileInfo(String fileId, String personName) {
		// TODO Auto-generated method stub
		try {
			assentFileInfoMapper.deleteById(fileId);
			pubLogMapper.saveNoramlLogInfo(personName, null, LogInfoDic.LOG_TRANSTYPE_ENUM.TRANSTYPE_DELETE.getCode(),
					String.format("删除附件编号:{%s} 操作成功!", new Object[] {fileId}),
					LogInfoDic.LOG_MODULAR_ENUM.EFOMS_ASSENT_MANAGEMENT.getMessage());
		} catch (Exception e) {
			// TODO: handle exception
			throw e;
		}
		return new ResultPojo();
	}

	/***
	 * 附件list查询；
	 * @param fileId 附件编号
	 * @param devId 设备编号
	 * @param devTypeCode 设备类型编号
	 * @param devCategoryCode 设备类别编号
	 * @return
	 */
	public ResultPojo getAssentFileInfoList(String fileId, String devId, String devTypeCode, String devCategoryCode) {
		// TODO Auto-generated method stub
		ResultPojo resultPojo = new ResultPojo();
		Map<String, Object> map = this.getSelectMapper(fileId, devId, devTypeCode, devCategoryCode);
		try {
			List<AssentFileInfo> list = this.assentFileInfoMapper.selectAssentFileInfoList(map);
			resultPojo.setResultList(list);
		} catch (Exception e) {
			// TODO: handle exception
			throw e;
		}
		return resultPojo;
	}
	
	/***
	 * 获取查询条件的map；
	 * @param fileId 附件编号
	 * @param devId 设备编号
	 * @param devTypeCode 设备类型编号
	 * @param devCategoryCode 设备类别编号
	 * @return
	 */
	private Map<String, Object> getSelectMapper(String fileId, String devId, String devTypeCode, String devCategoryCode){
		Map<String, Object> map = new HashMap<>();
		map.put("fileId", fileId);
		map.put("devId", devId);
		map.put("devTypeCode", devTypeCode);
		map.put("devCategoryCode", devCategoryCode);
		return map;
	}

}
